package de.tum.in.eist.poll.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

import de.tum.in.eist.poll.shared.Lecturer;

/***
 * view for the lecturer to manage lectures available for the survey
 */
public class ManageLecturesView {
	static void paint() {
		if(Poll.lecturer == null){
			History.newItem("lecturerLogin");	return;
		}
		RootPanel.get("logo").addStyleName("logo-small");
		final FlexTable lecTable = new FlexTable();
		lecTable.setWidget(0, 0, new HTML("<u>Lectures</u> <i>All Dates: (MM/DD/YYYY HH:MM)</i>"));
		lecTable.addStyleName("lecTable");
		Poll.pollService.getPolls(new AsyncCallback<List<de.tum.in.eist.poll.shared.Poll>>(){
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(List<de.tum.in.eist.poll.shared.Poll> result) {
				if(!result.isEmpty()){
					for(de.tum.in.eist.poll.shared.Poll p : result){
						lecTable.setText(lecTable.getRowCount(), 0, p.getTopic()+" on "+p.getDate().toString());
						// TODO: Delete Button
					}
				}
				final TextBox addDateBox = new TextBox();
				addDateBox.getElement().setAttribute("placeholder", "Date");
				lecTable.setWidget(lecTable.getRowCount(),0,addDateBox);
				final TextBox addStartTimeBox = new TextBox();
				addStartTimeBox.getElement().setAttribute("placeholder", "Start");
				lecTable.setWidget(lecTable.getRowCount(), 0, addStartTimeBox);
				lecTable.setText(lecTable.getRowCount()-1, 1, "-");
				final TextBox addEndTimeBox = new TextBox();
				addEndTimeBox.getElement().setAttribute("placeholder", "End");
				lecTable.setWidget(lecTable.getRowCount()-1, 2, addEndTimeBox);
				final Button addButton = new Button("Add Lecture");
				lecTable.setWidget(lecTable.getRowCount(), 2,addButton);
				class AddHandler implements ClickHandler, KeyUpHandler {
					public void onClick(ClickEvent event) {
						addToServer();
					}
					public void onKeyUp(KeyUpEvent event) {
						if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
							addToServer();
						}
					}
					public void addToServer() {
						de.tum.in.eist.poll.shared.Poll p = new de.tum.in.eist.poll.shared.Poll("",DateTimeFormat.getFormat("MM/dd/yyyy HH:mm").parse(addDateBox.getText()),
								DateTimeFormat.getFormat("MM/dd/yyyy HH:mm").parse(addStartTimeBox.getText()),
								DateTimeFormat.getFormat("MM/dd/yyyy HH:mm").parse(addEndTimeBox.getText()));
						Poll.adminService.addPoll(Poll.lecturer, p,
								new AsyncCallback<Void>() {
							public void onFailure(Throwable caught) {
							}
							public void onSuccess(Void result) {
								lecTable.setText(lecTable.insertRow(lecTable.getRowCount()-1),0,addDateBox.getText());
							}
						});
					}
				}
				AddHandler ah = new AddHandler();
				addDateBox.addKeyUpHandler(ah);
				addStartTimeBox.addKeyUpHandler(ah);
				addEndTimeBox.addKeyUpHandler(ah);
				addButton.addClickHandler(ah);
			}
		});
		RootPanel.get("container").add(lecTable);
	}
}
